/*
 * ChildPhoneGapForm.cpp
 *
 *  Created on: 2011. 4. 14.
 *      Author: JeeHoon
 */

#include <FApp.h>
#include "ChildPhoneGapForm.h"
#include "plugin/ChildBrowser.h"

ChildPhoneGapForm::ChildPhoneGapForm()
{
	__pChildBrowser = NULL;
}

ChildPhoneGapForm::ChildPhoneGapForm(ChildBrowser * pChildBrowser)
{
	__pChildBrowser = pChildBrowser;
}

ChildPhoneGapForm::~ChildPhoneGapForm()
{
}


result
ChildPhoneGapForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	int screen_width = 0;
	int screen_height = 0;

	Osp::Ui::Controls::Button * pDoneButton = NULL;

	/*screen*/
    if(E_SUCCESS == r) r = Osp::System::SystemInfo::GetValue("ScreenWidth", screen_width);
    if(E_SUCCESS == r) r = Osp::System::SystemInfo::GetValue("ScreenHeight", screen_height);

    /*Web*/
    if(NULL == __pWeb) __pWeb = new Osp::Web::Controls::Web();
    if(NULL == __pWeb) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) r = __pWeb->Construct(Osp::Graphics::Rectangle(0, 0, screen_width, screen_height));
	if(E_SUCCESS == r) r = AddControl(*__pWeb);
	if(E_SUCCESS == r) r = __pWeb->SetFocus();
	if(E_SUCCESS == r) __pWeb->SetLoadingListener(this);

	if(NULL == __pPluginManager) __pPluginManager = new PluginManager();
	if(NULL == __pPluginManager) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) __pPluginManager->Construct(this);


	if(NULL == pDoneButton) pDoneButton = new Osp::Ui::Controls::Button();
	if(NULL == pDoneButton) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) r = pDoneButton->Construct(Osp::Graphics::Rectangle(screen_width-100,screen_height-100,100,100),L"Done");
	if(E_SUCCESS == r) pDoneButton->SetActionId(CHILD_FORM_DONE_BUTTON);
	if(E_SUCCESS == r) pDoneButton->AddActionEventListener(*this);
	if(E_SUCCESS == r) r = AddControl(*pDoneButton);

	return r;
}


void
ChildPhoneGapForm::OnActionPerformed (const Osp::Ui::Control &source, int actionId)
{
	switch (actionId)
	{
	case CHILD_FORM_DONE_BUTTON:
		OnClose();
		Osp::App::Application::GetInstance()->SendUserEvent(EVENT_CHILD_PHONEGAP_PRESSED_DONE_BUTTON,NULL);
		break;
	default:
		break;
	}
}

bool
ChildPhoneGapForm::OnLoadingRequested (const Osp::Base::String &url, Osp::Web::Controls::WebNavigationType type)
{
	// insert URL
	__requests.Enqueue(url);

	if(true == url.StartsWith(L"gap://",0))
	{
		// insert Command
		__requests.Enqueue(url);
	}
	else
	{
		__isNativeReady = false;
	}

	return false;
}

void
ChildPhoneGapForm::OnLoadingCompleted(void)
{
	Osp::Base::String url;
	// remove URL
	__requests.Dequeue(url);
	OnLocationChange(url);

	/* native ready */
		if(__isNativeReady == false)
		{
			nativeReady();
			__isNativeReady = true;
		}

		while(0 < __requests.GetCount())
		{
			Osp::Base::String request;
			// remove Command
			__requests.Dequeue(request);
			__pPluginManager->processRequest(request);
		}	// while
}


void
ChildPhoneGapForm::OnLocationChange(Osp::Base::String url)
{
	__pChildBrowser->OnLocationChange(url);
}

void
ChildPhoneGapForm::OnClose()
{
	__pChildBrowser->OnClose();
}

void
ChildPhoneGapForm::OnOpenExternal()
{
	__pChildBrowser->OnOpenExternal();
}
